<HTML><HEAD><TITLE>read_token(+Stream, -Token, -Class)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">Character I/O</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>read_token(+Stream, -Token, -Class)</H1>
Succeeds if the next token from the input stream Stream is successfully
read and unified with Token and its token class with Class.


<DL>
<DT><EM>Stream</EM></DT>
<DD>Integer (stream number) or Atom (reserved or user-defined                symbolic stream name).
</DD>
<DT><EM>Token</EM></DT>
<DD>Variable or constant.
</DD>
<DT><EM>Class</EM></DT>
<DD>Variable or atom.
</DD>
</DL>
<H2>Description</H2>
   This predicate is an interface to the ECLiPSe tokenizer.  It can be used
   to read terms which are not ended by a fullstop or even to build whole
   new parsers.  The next token from the input stream Stream is read and
   unified with Token.  The token class of this token is unified with
   Class.

<P>
   The possible token classes with examples:

<P>
<PRE>
   ---------------------------------------
   | Input Example  Token    Class        |
   |------------------------------------  |
   | X              "X"      var          |
   | _              "_"      anonymous    |
   | abc            'abc'    atom         |
   | 'a-b'          'a-b'    quoted_atom  |
   | 123            123      integer      |
   | 1.2            1.2      float        |
   | 1_3            1_3      rational     |
   | 0.9__1.1       0.9__1.1 breal        |
   | "abc"          "abc"    string       |
   | |              "|"      solo         |
   | )              ")"      solo         |
   | (              ")"      solo         |
   | &lt;SPACE&gt;(       "("      open_par     |
   | ,              ','      comma        |
   | .&lt;NL&gt;          '.'      fullstop     |
   | 1e789&lt;NL&gt;      "1e789"  error        |
   ---------------------------------------|
</PRE>
   Note that round, square and curly brackets are solo tokens whose
   value is returned as a string.  Opening brackets preceded by space
   are treated specially as open_par tokens.  Comma and fullstop have
   their own token class.  All syntax errors are reported as class
   error, with the input string up to the error as Token.  The default
   error handler for the event 190 (reading EOF) returns end_of_file
   in both Class and Token.
<P>
   Note about signed numbers: the tokenizer returns a sign followed by a
   number as two separate tokens.  For instance, the input "-5" is read
   as two tokens, the atom '-' and the integer 5.  In the case of bounded
   reals, this leads to "-1.1__-0.9" being returned as the atom '-' and
   the bounded real 1.1__-0.9.  This is a non-canonical breal, since the
   upper bound is lower than the lower bound.  read_token/2,3 is the only
   predicate that can produce such objects, and it is the programmer's
   responsibility to construct a valid breal using breal_bounds/3 and
   breal_from_bounds/3, taking the sign into account.  Note that all
   other arithmetic operations are undefined on non-canonical breals.
<P>

<H3>Modes and Determinism</H3><UL>
<LI>read_token(+, -, -) is det
</UL>
<H3>Modules</H3>
This predicate is sensitive to its module context (tool predicate, see @/2).
<H3>Exceptions</H3>
<DL>
<DT><EM>(4) instantiation fault </EM>
<DD>Stream is not instantiated.
<DT><EM>(5) type error </EM>
<DD>Stream is not an atom or an integer.
<DT><EM>(5) type error </EM>
<DD>Class does not unify with an atom.
<DT><EM>(190) end of file reached </EM>
<DD>End of file was encountered before reading any character.
<DT><EM>(192) illegal stream mode </EM>
<DD>Stream is not an input stream.
<DT><EM>(193) illegal stream specification </EM>
<DD>Stream is an illegal stream specification.
<DT><EM>(198) reading past the file end </EM>
<DD>Trying to read even after the error 190 was raised.
</DL>
<H2>Examples</H2>
<PRE>
Success:
      [eclipse 1]: read_token(input,T,C).
              []
      T = []
      C = atom
      [eclipse 2]: read_token(input,T,C).
              [
      T = "["
      C = solo
      [eclipse 3]: read_token(input, "k",C).
              "k"
      C = string
      [eclipse 4]: read_token(input,T,C).
              X
      T = "X"
      C = var
      [eclipse 5]: read_token(input,T,C).
              1.6e-5.
      T = 1.6e-05
      C = float

Fail:
      [eclipse 6]: read_token(input, "[", C).
              &amp;
      no.

Error:
      [eclipse 7]: read_token(input, T, C).
              ^D
      T = end_of_file
      C = end_of_file
      yes. (Error 190, default handler)

      read_token(S, a(b,c), C).         (Error 4).
      read_token("string", a(b,c), C).  (Error 5).
      read_token(9, X + 2, C).          (Error 192). % stream not open
      read_token(atom, X + 2, C).       (Error 193).




</PRE>
<H2>See Also</H2>
<A HREF="../../kernel/syntax/get_chtab-2.html">get_chtab / 2</A>, <A HREF="../../kernel/syntax/set_chtab-2.html">set_chtab / 2</A>, <A HREF="../../kernel/iochar/read_token-2.html">read_token / 2</A>
</BODY></HTML>
